package com.mh.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.hibernate.validator.constraints.NotEmpty;

import com.mh.web.utils.DateJsonDeserializer;
import com.mh.web.utils.DateJsonSerializer;
import com.mh.web.utils.DateUtils;

/**
 * 
 * @ModelCoreReference [platform:/resource/demo-mh/.springDSL/com/mh/domain/AppUser.datatype]
 */

@Entity
@NamedQueries({
		@NamedQuery(name = "findAllAppUsers", query = "select myAppUser from AppUser myAppUser"),
		@NamedQuery(name = "findAppUserByBirthday", query = "select myAppUser from AppUser myAppUser where myAppUser.birthday = ?1"),
		@NamedQuery(name = "findAppUserByEmail", query = "select myAppUser from AppUser myAppUser where myAppUser.email = ?1"),
		@NamedQuery(name = "findAppUserByEmailContaining", query = "select myAppUser from AppUser myAppUser where myAppUser.email like ?1"),
		@NamedQuery(name = "findAppUserByFullName", query = "select myAppUser from AppUser myAppUser where myAppUser.fullName = ?1"),
		@NamedQuery(name = "findAppUserByFullNameContaining", query = "select myAppUser from AppUser myAppUser where myAppUser.fullName like ?1"),
		@NamedQuery(name = "findAppUserById", query = "select myAppUser from AppUser myAppUser where myAppUser.id = ?1"),
		@NamedQuery(name = "findAppUserByPrimaryKey", query = "select myAppUser from AppUser myAppUser where myAppUser.id = ?1"),
		@NamedQuery(name = "findAppUserBySecretPassword", query = "select myAppUser from AppUser myAppUser where myAppUser.secretPassword = ?1"),
		@NamedQuery(name = "findAppUserBySecretPasswordContaining", query = "select myAppUser from AppUser myAppUser where myAppUser.secretPassword like ?1"),
		@NamedQuery(name = "findAppUserByState", query = "select myAppUser from AppUser myAppUser where myAppUser.state = ?1"),
		@NamedQuery(name = "findAppUserByUserName", query = "select myAppUser from AppUser myAppUser where myAppUser.userName = ?1"),
		@NamedQuery(name = "findAppUserByUserNameContaining", query = "select myAppUser from AppUser myAppUser where myAppUser.userName like ?1") })				
@Table(schema = "MHTEST", name = "APP_USER")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "demo-mh/com/mh/domain", name = "AppUser")
public class AppUser implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 */

	@Column(name = "ID", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@XmlElement
	@SequenceGenerator(name = "seq_app_user", sequenceName = "seq_app_user")
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_app_user")	
	Integer id;

	/**
	 */

	@Column(name = "USER_NAME", length = 30, nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	@NotNull
	@NotEmpty
	@Size(max=30,  message="{com.vit.tamanho.maximo}")
	String userName;

	/**
	 */

	@Column(name = "FULL_NAME", length = 60, nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	@NotNull
	@NotEmpty
	@Size(max=60,  message="{com.vit.tamanho.maximo}")
	String fullName;

	/**
	 */

	@Column(name = "SECRET_PASSWORD", length = 30, nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	@NotNull
	@NotEmpty
	@Size(max=30,  message="{com.vit.tamanho.maximo}")
	String secretPassword;

	/**
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "BIRTHDAY", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	@NotNull
	
	Date birthday;

	/**
	 */

	@Column(name = "EMAIL", length = 60, nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	@NotNull
	@NotEmpty
	@Size(max=60,  message="{com.vit.tamanho.maximo}")
	String email;

	/**
	 */

	@Column(name = "STATE", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	@NotNull
	Boolean state;
	
	
	@ManyToMany
	@JoinTable(name = "MHTEST.APP_USER_ROLE",  
				joinColumns = @JoinColumn(name="USER_NAME",  referencedColumnName = "USER_NAME" ), 
				inverseJoinColumns = {@JoinColumn(name = "APPLICATION", columnDefinition = "APPLICATION", referencedColumnName = "APPLICATION"),
										@JoinColumn(name = "ROLE_NAME", referencedColumnName = "ROLE_NAME", columnDefinition = "ROLE_NAME")
									 })
	private List<AppRole> appRole = new ArrayList<AppRole>(0);

	// Constructors

	@JsonIgnore
	public List<AppRole> getAppRole() {
		return appRole;
	}

	public void setAppRole(List<AppRole> appRole) {
		this.appRole = appRole;
	}

	/**
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 */

	public Integer getId() {
		return this.id;
	}

	/**
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 */
	public String getUserName() {
		return this.userName;
	}

	/**
	 */
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	/**
	 */
	public String getFullName() {
		return this.fullName;
	}

	/**
	 */
	public void setSecretPassword(String secretPassword) {
		this.secretPassword = secretPassword;
	}

	/**
	 */
	public String getSecretPassword() {
		return this.secretPassword;
	}

	/**
	 */
	@JsonDeserialize(using=DateJsonDeserializer.class)
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	/**
	 */
	@JsonSerialize(using=DateJsonSerializer.class)	
	public Date getBirthday() {
		return this.birthday;
	}

	/**
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 */
	public String getEmail() {
		return this.email;
	}

	/**
	 */
	public void setState(Boolean state) {
		this.state = state;
	}

	/**
	 */
	public Boolean getState() {
		return this.state;
	}

	/**
	 */
	public AppUser() {
	}

	/**
	 * Copies the contents of the specified bean into this bean.
	 *
	 */
	public void copy(AppUser that) {
		setId(that.getId());
		setUserName(that.getUserName());
		setFullName(that.getFullName());
		setSecretPassword(that.getSecretPassword());
		setBirthday(that.getBirthday());
		setEmail(that.getEmail());
		setState(that.getState());
	}
	
	
	public String getBirthdayString() {
		return DateUtils.convertToString(this.birthday);
	}
	
	public void setBirthdayString(String str){
		this.birthday = DateUtils.convertToDate(str);
	}

	/**
	 * Returns a textual representation of a bean.
	 *
	 */
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");
		buffer.append("userName=[").append(userName).append("] ");
		buffer.append("fullName=[").append(fullName).append("] ");
		buffer.append("secretPassword=[").append(secretPassword).append("] ");
		buffer.append("birthday=[").append(birthday).append("] ");
		buffer.append("email=[").append(email).append("] ");
		buffer.append("state=[").append(state).append("] ");

		return buffer.toString();
	}

	/**
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	/**
	 */
	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof AppUser))
			return false;
		AppUser equalCheck = (AppUser) obj;
		if ((id == null && equalCheck.id != null) || (id != null && equalCheck.id == null))
			return false;
		if (id != null && !id.equals(equalCheck.id))
			return false;
		return true;
	}
}
